package jk.zmn.server.core;

import java.io.IOException;
import java.net.Socket;

import jk.zmn.server.utils.CloseUtils;

/**
 * 分发器
 * @author zmn
 *
 */
public class Dispatcher implements Runnable{

	private Request request;
	private Response response;
	private Socket client;
	
	private int code=200;
	
	public Dispatcher(Socket client) {
		try {
			client = client;
			request = new Request(client.getInputStream());
			response = new Response(client.getOutputStream());
		} catch (IOException e) {
			code=500;
			return;
		}
	}
	
	
	@Override
	public void run() {
		
		/*Servlet servlet = new Servlet();
		
		servlet.service(request, response);
		*/
		
		Servlet servlet = WebApp.getServlet(request.getUrl());
		if (servlet==null) {
			this.code =404;
		}else {
			servlet.service(request, response);
		}
		
		response.pushToclient(code);
		
		CloseUtils.closeSocket(client);
	}

}
